package com.hy.heath.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hy.heath.common.BusinessException;
import com.hy.heath.common.Result;
import com.hy.heath.pojo.entity.HealthKnowledge;
import com.hy.heath.service.HealthKnowledgeService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/api/health-knowledge")
public class HealthKnowledgeController {

    @Autowired
    private HealthKnowledgeService knowledgeService;

    // 分页查询
    @GetMapping("/page")
    public Result<Page<HealthKnowledge>> page(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) String orderBy) {
        Page<HealthKnowledge> page = knowledgeService.pageKnowledge(
                new Page<>(pageNum, pageSize), category, orderBy);
        return Result.success(page);
    }

    // 按分类查询
    @GetMapping("/category/{category}")
    public Result<List<HealthKnowledge>> listByCategory(@PathVariable String category) {
        return Result.success(knowledgeService.listByCategory(category));
    }

    // 按作者查询
    @GetMapping("/author/{author}")
    public Result<List<HealthKnowledge>> listByAuthor(@PathVariable String author) {
        return Result.success(knowledgeService.listByAuthor(author));
    }

    // 查看详情（带访问量递增）
    @GetMapping("/{id}")
    public Result<HealthKnowledge> detail(@PathVariable Integer id) {
        HealthKnowledge knowledge = knowledgeService.getById(id);
        if (knowledge == null) {
            throw new BusinessException("健康知识不存在");
        }
        knowledgeService.incrementViewCount(id);
        return Result.success(knowledge);
    }
}
